#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>

using namespace std;

const int MAXREC = 100;
unsigned char recs[MAXREC][8];
int recNum;
unsigned char rows[8];
bool colUsed[8];
bool majorDiagUsed[15];
bool minorDiagUsed[15];

void search(int rowNum) {
	if (rowNum == 8) {
		memcpy(recs[recNum++], rows, 8);
		return;
	}
	for (int colNum = 0; colNum < 8; ++colNum) {
		if (!colUsed[colNum] && !majorDiagUsed[colNum-rowNum+7] && !minorDiagUsed[rowNum+colNum]) {
			rows[rowNum] = colNum;
			colUsed[colNum] = true;
			majorDiagUsed[colNum-rowNum+7] = true;
			minorDiagUsed[rowNum+colNum] = true;
			search(rowNum+1);
			colUsed[colNum] = false;
			majorDiagUsed[colNum-rowNum+7] = false;
			minorDiagUsed[rowNum+colNum] = false;
		}
	}
}

int main(void)
{
	recNum = 0;
	memset(colUsed, 0, 8);
	memset(majorDiagUsed, 0, 15);
	memset(minorDiagUsed, 0, 15);
	search(0);
	int n;
	cin >> n;
	int chessboard[8][8];
	for (int testcase = 0; testcase < n; ++testcase) {

		for (int i=0; i<8; ++i)
			for (int j=0; j<8; ++j)
				cin >> chessboard[i][j];
		int maxval = -1;
		for (int i=0; i<recNum; ++i) {
			int sum = 0;
			for (int j=0; j<8; ++j)
				sum += chessboard[j][recs[i][j]];
			if (sum > maxval) maxval = sum;
		}
		cout << setw(5) << maxval << endl;
	}
	return 0;
}